
package com.hlkj.pay.service.merchant.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.id.IdUtils;
import com.hlkj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.merchant.report.MerchantReportAmountPageDto;
import com.hlkj.pay.dto.merchant.report.MerchantReportAmountPageQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.mapper.merchant.MerchantReportAmountMapper;
import com.hlkj.pay.infrastructure.mapper.merchant.MerchantReportInfoMapper;
import com.hlkj.pay.infrastructure.model.merchant.MerchantReportAmountDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.merchant.IMerchantReportAmountService;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * @author HlpayTeam
 * @date 2024/12/25 14:08
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MerchantReportAmountServiceImpl extends CommonSnFilterService implements IMerchantReportAmountService {

    private final MerchantReportInfoMapper merchantReportInfoMapper;

    private final MerchantReportAmountMapper merchantReportAmountMapper;

    @Override
    public void addReportAmount(MerchantReportAmountDO merchantReportAmount) {
        merchantReportAmount.setCode(IdUtils.getSnowflakeNextIdStr());
        merchantReportAmount.setProcessStatus(MerchantEnum.REPORT_AMOUNT_PROCESS_STATUS.INIT.getCode());
        merchantReportAmountMapper.insert(merchantReportAmount);
    }

    @Override
    public MerchantReportAmountDO queryAmountByReportId(Long reportId, MerchantEnum.REPORT_AMOUNT_PROCESS_STATUS reportAmountStatus) {
        MerchantReportAmountDO merchantReportAmountDO = new MerchantReportAmountDO();
        merchantReportAmountDO.setReportId(reportId);
        merchantReportAmountDO.setProcessStatus(reportAmountStatus.getCode());
        LambdaQueryWrapper<MerchantReportAmountDO> lambdaQueryWrapper = buildReportAmountWrapper(merchantReportAmountDO);
        lambdaQueryWrapper.last("limit 1");
        return merchantReportAmountMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public void updateReportAmount(MerchantReportAmountDO merchantReportAmount) {
        merchantReportAmountMapper.updateById(merchantReportAmount);
    }

    @Override
    public MerchantReportAmountDO queryAmountByCode(String code) {
        return merchantReportAmountMapper.selectOne(MerchantReportAmountDO::getCode, code);
    }

    @Override
    public PageResult<MerchantReportAmountPageDto> queryAmountPage(MerchantReportAmountPageQueryDto amountPageQueryDto) {
        if (!StringUtils.hasText(amountPageQueryDto.getSortingFields())) {
            amountPageQueryDto.setSortingFields("a.id");
        }
        IPage<MerchantReportAmountPageDto> mpPage = MyBatisUtils.buildPage(amountPageQueryDto);
        QueryWrapper<MerchantReportAmountPageDto> queryWrapper = buildInfoWrapper(amountPageQueryDto);
        // queryWrapper.orderByDesc("a.id");
        merchantReportInfoMapper.selectMerchantReportAmountPage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }

    /**
     * 查询条件封装
     *
     * @param merchantReportAmountDO
     * @return
     */
    LambdaQueryWrapper<MerchantReportAmountDO> buildReportAmountWrapper(MerchantReportAmountDO merchantReportAmountDO) {
        LambdaQueryWrapperX<MerchantReportAmountDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eqIfPresent(MerchantReportAmountDO::getReportId, merchantReportAmountDO.getReportId());
        queryWrapper.eqIfPresent(MerchantReportAmountDO::getProcessStatus, merchantReportAmountDO.getProcessStatus());
        queryWrapper.eqIfPresent(MerchantReportAmountDO::getCode, merchantReportAmountDO.getCode());
        return queryWrapper;
    }

    /**
     * 查询条件封装
     *
     * @param amountPageQueryDto
     * @return
     */
    QueryWrapper<MerchantReportAmountPageDto> buildInfoWrapper(MerchantReportAmountPageQueryDto amountPageQueryDto) {
        filterSn(amountPageQueryDto);
        QueryWrapperX<MerchantReportAmountPageDto> queryWrapper = new QueryWrapperX<>();
        queryWrapper.eq("a.delete_flag", CommonEnum.YES_NO_TYPE.NO.getCode());
        queryWrapper.eqIfPresent("b.sn", amountPageQueryDto.getSn());
        queryWrapper.likeIfPresent("b.sn", amountPageQueryDto.getLikeSn());
        queryWrapper.inIfPresent("b.sn", amountPageQueryDto.getSnList());
        queryWrapper.eqIfPresent("b.user_id", amountPageQueryDto.getLikeUserId());
        queryWrapper.eqIfPresent("a.status", amountPageQueryDto.getStatus());
        queryWrapper.eqIfPresent("a.process_status", amountPageQueryDto.getProcessStatus());
        queryWrapper.geIfPresent("a.create_time", amountPageQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent("a.create_time", amountPageQueryDto.getEndQueryTime());
        return queryWrapper;
    }
}
